package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.pojo.DtsCategory;
import com.qf.service.DtsCategoryService;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author 邢式瑞
 * @version 1.0
 * @project E-commercebackGroundManagementSystem
 * @date 2022/9/24 9:30
 */
@Service
public class DtsCategoryServiceImpl  implements DtsCategoryService {
    @Autowired
    private DtsCategoryMapper dtsCategoryMapper;
    @Override
    public Set<CatVo> selectCategoryList() {
        List<DtsCategory> dtsCategories = dtsCategoryMapper.selectList(null);
       return   dtsCategories.stream().map(dtsCategory -> {
            CatVo catVo = new CatVo();
            catVo.setLabel(dtsCategory.getName());
            catVo.setValue(dtsCategory.getId());
            return catVo;
        }).collect(Collectors.toSet());

    }

    @Override
    public Page<DtsCategory> selectPage(Integer page, Integer limit, String sort, Integer id, String name, String order) {

        Page<DtsCategory> dtsCategoryPage = new Page<DtsCategory>(page, limit);
        QueryWrapper<DtsCategory> dtsCategoryQueryWrapper = new QueryWrapper<>();
        dtsCategoryQueryWrapper.orderBy(!StringUtils.isEmpty(sort),"asc".equalsIgnoreCase(order),sort).like(!StringUtils.isEmpty(name),"name",name).eq(id!=null,"id",id).eq("deleted",false);
        return dtsCategoryMapper.selectPage(dtsCategoryPage,dtsCategoryQueryWrapper);
    }



    @Override
    public Set<CatVo> selectParentCatVo() {
        List<DtsCategory> pids = dtsCategoryMapper.selectList(new QueryWrapper<DtsCategory>().eq("level", "L1"));
      return   pids.stream().map(pid->{
            CatVo catVo = new CatVo();
            catVo.setValue(pid.getId());
            catVo.setLabel(pid.getName());
            return catVo;
        }).collect(Collectors.toSet());
       // return null;
    }

    @Override
    public void insert(DtsCategory dtsCategory) {
        dtsCategoryMapper.insert(dtsCategory);
    }

    @Override
    public void update(DtsCategory dtsCategory) {
        dtsCategoryMapper.updateById(dtsCategory);
    }

    @Override
    public void delete(DtsCategory dtsCategory) {
          dtsCategoryMapper.updateById(dtsCategory);
    }

    @Override
    public Set<Integer> selectById(Integer categoryId) {
        DtsCategory dtsCategory = dtsCategoryMapper.selectOne(new QueryWrapper<DtsCategory>().eq("id", categoryId));
        HashSet<Integer> categoryIds = new HashSet<>();
        categoryIds.add(categoryId);
        categoryIds.add(dtsCategory.getPid());
        return categoryIds;
    }

    @Override
    public Set<CatVo> selectCategoryAllList() {
        //son
        List<DtsCategory> collect = dtsCategoryMapper.selectList(new QueryWrapper<DtsCategory>().eq("level", "L2"));

        Set<CatVo> catVos = new HashSet<>();
//        List<DtsCategory> collect = dtsCategories.stream().filter(dtsCategory -> {
//            if (dtsCategory.getPid() == 0) {
//                return false;
//            } else return true;
//        }).collect(Collectors.toList());
        //par
        List<DtsCategory> pids = dtsCategoryMapper.selectList(new QueryWrapper<DtsCategory>().eq("level", "L1"));
//        Set<CatVo> father = pids.stream().map(pid -> {
//            CatVo catVo = new CatVo();
//            catVo.setValue(pid.getId());
//            catVo.setLabel(pid.getName());
//            return catVo;
//        }).collect(Collectors.toSet());
//
//        for (CatVo pid : father) {
//            List<CatVo>list=new ArrayList<>();
//            for (DtsCategory catVo : collect  ) {
//                if(pid.getValue().equals(catVo.getPid())){
//                        CatVo cur = new CatVo();
//                        cur.setValue(catVo.getId());
//                        cur.setLabel(catVo.getName());
//                        list.add(cur);
//                }
//            }
//            pid.setChildren(list);
//        }
//        return father;


        for (DtsCategory pid : pids) {
            CatVo catVo = new CatVo();
            catVo.setValue(pid.getId());
            catVo.setLabel(pid.getName());
            ArrayList<CatVo> catVoList = new ArrayList<>();
            for (DtsCategory dtsCategory : collect) {
                if(pid.getId().equals(dtsCategory.getPid())){
                    CatVo catVo1 = new CatVo();
                    catVo1.setValue(dtsCategory.getId());
                    catVo1.setLabel(dtsCategory.getName());
                    catVoList.add(catVo1);
                }
            }
            catVo.setChildren(catVoList);
            catVos.add(catVo);
        }

        return catVos;
    }
//    public  Set<CatVo> findChildren(List<DtsCategory> dtsCategories){
//        for (DtsCategory dtsCategory : dtsCategories) {
//            for (DtsCategory dtsCategory1 : dtsCategories) {
//                if(dtsCategory.getId()==dtsCategory1.getPid()){
//
//                }
//            }
//        }
//
//        }

    }

